/*
 * 创建日期 2010-1-25
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.cpp.core.tag;

import java.io.Writer;
import java.util.List;

import javax.servlet.jsp.JspException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import  org.fantasy.cpp.core.item.ItemSupport;
import  org.fantasy.cpp.core.pojo.QueryItem;
import  org.fantasy.cpp.core.pojo.QueryPage;
import  org.fantasy.cpp.web.controller.QueryController;
import  org.fantasy.common.util.WebUtil;

/**
 *  查询模块标签
 * @author: 王文成
 * @version: 1.0
 * @since 2010-1-25
 */
public class QueryModuleTag extends AbstractTag {
	private static final Log log = LogFactory.getLog(QueryModuleTag.class);

	private static final int ITEM_BASE = 3;
	
	public void setModuleId(String moduleId){
		
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			Writer writer = getJspWriter();
			writer.write(getQryHtml());
			writer.write(getQryScript());
			//log.debug(getQryHtml());
		} catch (Exception e) {
			e.printStackTrace();
			throw new JspException(e.getMessage());
		}
		return EVAL_PAGE;
	}

	/**
	 * 取得查询页HTML
	 * 
	 * @return
	 */
	protected String getQryHtml() throws Exception {
		StringBuffer html = new StringBuffer(1024);
		List<QueryItem> items = getItems();
		if (!items.isEmpty()) {
			html.append("<table border=\"0\" cellspacing=\"3\" cellpadding=\"0\" class=\"etable\"> \n");
			html.append(getShowHtml(items));
			html.append("<tbody class=\"qry_hide\"> \n");
			html.append(getHideHtml(items));
			html.append("</tbody> \n");
			html.append("</table> \n");
		}
		return html.toString();
	}

	/**
	 * 取得查询页HTML查询条件的默认显示部分，取所有查询条件的前面几个形成一行，个数由ITEM_BASE决定。
	 * 查询条件的个数由之前取查询条件的方法来保证，不足的会用null填充。
	 * 
	 * @return
	 */
	protected String getShowHtml(List<QueryItem> items) throws Exception {
		StringBuffer html = new StringBuffer(1024);
		if (items.isEmpty()) {
			html.append("<tr><td></td></tr>");
		} else {
			html.append("<tr>\n");
			for (int i = 0; i < ITEM_BASE; i++) {
				QueryItem bean = items.get(i);
				html.append("<td>\n");
				html.append(getItemHtml(bean));
				html.append("</td>\n");
			}

			html.append("</tr>\n");
		}
		return html.toString();
	}

	/**
	 * 取得查询条件隐藏部分 查询条件的隐藏部分，每ITEM_BASE个数作为一行
	 * 
	 * @return
	 */
	protected String getHideHtml(List<QueryItem> items) throws Exception {
		StringBuffer html = new StringBuffer(1024);
		for (int i = ITEM_BASE, k = 1; i < items.size(); i++, k++) {
			QueryItem bean = items.get(i);
			if (k == 1) {
				html.append("<tr>\n");
			}
			html.append("<td>\n");
			html.append(getItemHtml(bean));
			html.append("</td>\n");
			if (k == ITEM_BASE) {
				html.append("</tr>\n");
				k = 0; // 因为进入下一轮循环前会执行i++, k++
			}
		}
		return html.toString();
	}

	/**
	 * 取得查询条件,根据ITEM_BASE用null补齐个数
	 * 
	 * @return
	 */
	protected List<QueryItem> getItems() throws Exception {
		QueryPage info = getPageInfo();
		List<QueryItem> items = info.getQueryItems();
		int more = items.size() % ITEM_BASE;
		if (more > 0)
			more = ITEM_BASE - more; // 需要补齐的时候进行按ITEM_BASE取补
		for (int i = 0; i < more; i++)
			items.add(null);
		return items;
	}

	/**
	 * 取得查询条件HTML
	 * 
	 * @param item
	 * @return
	 */
	protected String getItemHtml(QueryItem item) throws Exception {
		if (item == null)
			return "";
		Object obj = Class.forName(item.getClassName()).newInstance();
		ItemSupport instance = (ItemSupport) obj;
		instance.setItem(item);
		instance.setHttpServletRequest(getRequest());
		return instance.getHtml();
	}
	
	/**
	 * 取得页面信息
	 * @return
	 * @throws Exception
	 */
	private QueryPage getPageInfo() throws Exception {
		return (QueryPage) WebUtil.findAttribute(getRequest(),QueryController.PAGE_INFO);
	}

	/**
	 * 取得脚本信息
	 * 
	 * @return
	 * @throws Exception
	 */
	private String getQryScript() throws Exception {
		QueryPage info = getPageInfo();
		StringBuffer html = new StringBuffer(1024);
		String script = (info.getMainScript() + "").toLowerCase();
		boolean isInclude = script.contains("<script") && script.contains("</script>");
		if (!isInclude) {
			html.append("<script type=\"text/javascript\"> \n");
		}
		html.append(info.getMainScript() + " \n");
		if (!isInclude) {
			html.append("</script> \n");
		}
		return html.toString();
	}
}